<?php
/**
 * 商品栏目管理
 *
 *
 ***/

defined('InShopNC') or exit('Access Invalid!');
class goods_templatesControl extends SystemControl{
    //const EXPORT_SIZE = 5000;
    public function __construct() {
        parent::__construct ();
        //Language::read('goods');
    }
    
    /**
     * 模板列表
     * 
     */
    public function indexOp(){
        $gt_class_model=Model('gt_class');
        $condition=0;
        $gt_class_list=$gt_class_model->gt_class_list($condition);
        Tpl::output('gt_class_list',$gt_class_list);
        Tpl::showpage('gt_class.list');
    }
    
    /**
     * 修改模板状态
     */
    public function change_statusOp(){
        $gt_class_model=Model('gt_class');
        if ($_POST['is_active']==1){
            $data['is_active']=0;
        }else if($_POST['is_active']==0){
            $data['is_active']=1;
          
        }
        
        $condition['gt_class_id']=$_POST['gs_tp_id'];
        //查找是否有已经开启的模板
        $result=$gt_class_model->editGt_class($condition,$data);

        if ($result){
            $data['status']=1;
            //$daat['is_active']=$data['is_active'];
        }else{
            $data['status']=0;
        }
        echo json_encode($data);die;
    }
    
    public function delete_contentOp(){
        $condition['gt_class_id']=$_POST['gt_class_id'];
        $goods_templates_model=Model('goods_templates');
        $result=$goods_templates_model->delete_content($condition);
        if ($result){
            
            $data['status']=1;
            //$daat['is_active']=$data['is_active'];
        }else{
            $data['status']=0;
        }
        echo json_encode($data);die;
    }
    
    /**
     * 新增模板
     */
    public function addGt_classOp(){
        //模板名称，模板设定的商品数量
        if(!empty($_POST)){//保存数据
            //展开数据
            $data['gt_class_name']=$_POST['gt_class_name'];
            $data['is_active']=0;
            $gt_class_model=Model('gt_class');
            $result=$gt_class_model->addGt_class($data);
            if ($result){
               $gt_class_model=Model('gt_class');
                $condition=0;
                $gt_class_list=$gt_class_model->gt_class_list($condition);
                Tpl::output('gt_class_list',$gt_class_list);
                Tpl::showpage('gt_class.list');
            }else{
                //保存失败
            }
            
        }else{
            Tpl::showpage('gt_class.add');//显示页面
        }
        
    }
    
    public function goods_templates_listOp(){
        $goods_templates_model=Model('goods_templates');
        $condition['gt_class_id']=0;//得到对应的模板id
        $goods_templates_list=$goods_templates_model->goods_templates_list($condition);
        //放上商品图片和名称
        foreach ($goods_templates_list as $key =>$val){
            $gs_tp_image=UPLOAD_SITE_URL.'/'.GT_TEMPLATES.'/'.$val['gs_tp_image'];
            $goods_templates_list[$key]['gs_tp_image']=$gs_tp_image;
            if ($val['is_goods']){
                $goods_model=Model('goods');
                $condition_go['goods_id']=$val['goods_id'];
                $goods_info=$goods_model->getGoodsInfo($condition_go);
                $goods_templates_list[$key]['goods_name']=$goods_info['goods_name'];
                $image=cthumb($goods_info['goods_image'],240,$goods_info['store_id']);
                $goods_templates_list[$key]['goods_image']=$image;
            }
            if($val['is_voucher']){
                $voucher_model=Model('voucher');
                $condition_vr['voucher_t_id']=$val['voucher_t_id']; 
                $voucher_info=$voucher_model->getVoucher_template($condition_vr);
                //var_export($voucher_info);die;
                $goods_templates_list[$key]['voucher']=$voucher_info['voucher_t_title'].'--满'.$voucher_info['voucher_t_limit'].'减'.$voucher_info['voucher_t_price'];
                
            }
           
        }

        Tpl::output('goods_templates_list',$goods_templates_list);
        Tpl::showpage('goods_templates.list');//显示页面
    }
    
    /**
     * 编辑、添加、保存
     */
    public function goods_templates_addOp(){
        if ($_POST['gt_class_id']){
            $goods_templates_model=Model('goods_templates');
            //保存
            $data['gs_tp_id']=$_POST['gs_tp_id'];
            $data['goods_id']=$_POST['goods_id'];
            $data['gt_class_id']=$_POST['gt_class_id'];
            $data['is_goods']=$_POST['is_goods'];
            $data['goods_size']=$_POST['goods_size'];
            $data['is_voucher']=$_POST['is_voucher'];
            //$data['gs_tp_image']=$_POST['gs_tp_image'];
            $data['gs_tp_image_width']=$_POST['gs_tp_image_width'];
            $data['gs_tp_image_height']=$_POST['gs_tp_image_height'];
            $data['voucher_t_id']=$_POST['voucher_t_id'];
            $data['is_adv']=$_POST['is_adv'];
            $data['adv_size']=$_POST['adv_size'];
            $data['goods_big_num']=$_POST['goods_big_num'];
            $data['goods_small_num']=$_POST['goods_small_num'];
            $upload		= new UploadFile();
            if($_FILES['gs_tp_image']['name'] != '' ){

                $upload->set('default_dir',GT_TEMPLATES);
                $result = $upload->upfile('gs_tp_image');
                if (!$result){
                    showMessage($upload->error,'','','error');
                }
                $data['gs_tp_image'] = $upload->file_name;
                
            }
            
            $result=$goods_templates_model->addGoods_templates($data);
            if ($result){
                //成功
                $goods_templates_list=$goods_templates_model->goods_templates_list(array('gt_class_id'=>$data['gt_class_id']));
                
                //放上商品图片和名称
                foreach ($goods_templates_list as $key =>$val){
                    $gs_tp_image=UPLOAD_SITE_URL.'/'.GT_TEMPLATES.'/'.$val['gs_tp_image'];
                    $goods_templates_list[$key]['gs_tp_image']=$gs_tp_image;
                    if ($val['is_goods']){
                        $goods_model=Model('goods');
                        $condition_go['goods_id']=$val['goods_id'];
                        $goods_info=$goods_model->getGoodsInfo($condition_go);
                        $goods_templates_list[$key]['goods_name']=$goods_info['goods_name'];
                        $image=cthumb($goods_info['goods_image'],240,$goods_info['store_id']);
                        $goods_templates_list[$key]['goods_image']=$image;
                    }
                    if($val['is_voucher']){
                        $voucher_model=Model('voucher');
                        $condition_vr['voucher_t_id']=$val['voucher_t_id'];
                        $voucher_info=$voucher_model->getVoucher_template($condition_vr);
                        //var_export($voucher_info);die;
                        $goods_templates_list[$key]['voucher']=$voucher_info['voucher_t_title'].'--满'.$voucher_info['voucher_t_limit'].'减'.$voucher_info['voucher_t_price'];
                
                    }
                     
                }
                
                
                Tpl::output('goods_templates_list',$goods_templates_list);
                Tpl::showpage('goods_templates.list');//显示页面
            }else{
               showMessage("新增失败，请联系开发人员");
            }
        }else{
            //echo 11;die;
            //显示表单
            if($_GET['gs_tp_id']){
                //编辑，找出对应gtp
                $where['gs_tp_id']=$_GET['gs_tp_id'];
                $goods_templates_model=Model('goods_templates');
                $goods_templates_info=$goods_templates_model->goods_templates_info($where);
                Tpl::output('goods_templates_info',$goods_templates_info);
                
                
            }
            $gt_class_model=Model('gt_class');
            $condition=0;
            $gt_class_list=$gt_class_model->gt_class_list($condition);
            //查找代金券列表
            $voucher_model=Model('voucher');
            $where['voucher_t_state']=1;
            $voucher_templates_list=$voucher_model->getVoucherTemplateList($where);
            Tpl::output('voucher_templates_list',$voucher_templates_list);
            Tpl::output('gt_class_list',$gt_class_list);
            Tpl::showpage('goods_templates.add');//显示页面
        }
    }
    
    public function goods_listOp(){
        $model_goods = Model('goods');
        $condition = array();
        if ($_POST['skip']==0){
            $start=intval($_POST['skip']);//1
        }else{
            $start=intval($_POST['skip'])*8;//1
        }

        //skip=2:2*2=4,(2+1)*2=6
        $length=8;
        $limit= " $start,$length ";
        $count=0;
        if (empty($_POST['goods_name'])){
            $condition['goods_name'] = null;
        }else{
            $condition['goods_name'] = $_POST['goods_name']; 
        }
        $goods_list=$model_goods->get_GoodsList($condition,$limit);
        $count=count($goods_list);
        if (!empty($goods_list)){
            foreach ($goods_list as $key=>$val){
                $image=cthumb($val['goods_image'],240,$val['store_id']);
                $goods_list[$key]['goods_image']=$image;
            }
            $data['goods_list']=$goods_list;
            $data['skip']=$_POST['skip'];
            $data['status']=1;
            $data['post']=$_POST;
        }else{
            $data['status']=0;
        }
        
        echo json_encode($data);die;
                
    }
    
}